package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;

@SuppressWarnings("serial")
public class StuUpdateDialog extends JDialog implements ActionListener{  
    //=========面板控件  
    //......左侧标题栏  
    private JLabel snoLab,snameLab,ssexLab,sageLab,sdeptLab,sclassLab,sphoneLab;  
    //......右侧信息选择填写栏  
    private JTextField snoTxt,snameTxt,ssexTxt,sageTxt,sdeptTxt,sclassTxt,sphoneTxt;  
    //......添加和取消按钮  
    private JButton addBtn,cancelBtn;  
    //......布局控件  
    private JPanel left,center,bottom;  
      
    //构造函数  
    public StuUpdateDialog(JFrame owner, String title, boolean modal, StuModel sm, int rowNum)   
    {  
        //========重写父类方法  
        super(owner, title, modal);  
        //========左侧标签栏  
        snoLab = new JLabel("学号: ");  
        snameLab = new JLabel("姓名: ");  
        ssexLab = new JLabel("性别: ");  
        sageLab = new JLabel("年龄: ");  
        sdeptLab = new JLabel("专业: ");  
        sclassLab = new JLabel("班级: "); 
        sphoneLab = new JLabel("高数成绩：");
        //========右侧信息填写栏  
        snoTxt = new JTextField();     
        snoTxt.setText(sm.getValueAt(rowNum, 0).toString());  
        snoTxt.setEditable(false);  
        snameTxt = new JTextField();  
        snameTxt.setText((String)sm.getValueAt(rowNum, 1));  
        ssexTxt = new JTextField();  
        ssexTxt.setText((String)sm.getValueAt(rowNum, 2));  
        sageTxt = new JTextField();  
        sageTxt.setText(sm.getValueAt(rowNum, 3).toString());  
        sdeptTxt = new JTextField();  
        sdeptTxt.setText((String)sm.getValueAt(rowNum, 4));  
        sclassTxt = new JTextField();  
        sclassTxt.setText((String)sm.getValueAt(rowNum, 5));  
        sphoneTxt = new JTextField();  
        sphoneTxt.setText((String)sm.getValueAt(rowNum, 6));  
        //========添加和取消按钮  
        addBtn = new JButton("修改");  
        cancelBtn = new JButton("取消");  
        //......添加监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("update");  
        cancelBtn.addActionListener(this);  
        cancelBtn.setActionCommand("cancel");  
        //========创建布局  
        //......创建左边栏  
        left = new JPanel();  
        left.setLayout(new GridLayout(7, 1));  
        left.add(snoLab);  left.add(snameLab);   
        left.add(ssexLab); left.add(sageLab);   
        left.add(sdeptLab);  left.add(sclassLab); 
        left.add(sphoneLab);
        //......创建右边栏  
        center = new JPanel();  
        center.setLayout(new GridLayout(7, 1));  
        center.add(snoTxt);  center.add(snameTxt);  
        center.add(ssexTxt); center.add(sageTxt);  
        center.add(sdeptTxt);  center.add(sclassTxt); 
        center.add(sphoneTxt);
        //========底层添加和取消按钮  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(cancelBtn);  
        //========整体布局  
        this.add(left,BorderLayout.WEST);  
        this.add(center,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
          
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(450,300);
		this.setLocationRelativeTo(null);
		setVisible(true);  
    }  
  
    public void actionPerformed(ActionEvent e) {  
        // TODO Auto-generated method stub  
        if(e.getActionCommand().equals("update")) {  
        String sql = "update student set Sname=?,Ssex=?,Sage=?,Sdept=?,Sclass=?,Sphone=? where Sno=?"; 
            String sname=snameTxt.getText();
            String ssex=ssexTxt.getText();
            String sdept=sdeptTxt.getText();
            int sage=Integer.parseInt(sageTxt.getText());
            String sclass=sclassTxt.getText();
            String sphone=sphoneTxt.getText();
            int sno=Integer.parseInt(snoTxt.getText());
            BaseDao baseDao=new BaseDao();
            int num=baseDao.update(sql, sname,ssex,sdept,sage,sclass,sphone,sno);
            if(num>0) {
				JOptionPane.showMessageDialog(null, "更新成功！");
			}else {
				JOptionPane.showMessageDialog(null, "更新失败，请输入正确的信息！");
			}
            //========关闭窗口  
            this.dispose();  
        } else if(e.getActionCommand().equals("cancel")) {  
            //========关闭窗口  
            this.dispose();  
        }  
    }  
}  
